library(tidyverse)

brasil <- read_csv2("app/data/brasil.csv")
data <- read_csv2("app/data/amostra.csv", na = "NA",
                     col_types = cols(
                         st_acidente_feriado = col_character(),
                         ds_agente_causador = col_character(),
                         ano_cat = col_integer(),
                         ds_cnae_classe_cat = col_character(),
                         dt_acidente = col_date(format = "%d/%m/%Y"),
                         st_dia_semana_acidente = col_character(),
                         ds_emitente_cat = col_character(),
                         hora_acidente = col_time(format = "%H%M"),
                         idade_cat = col_integer(),
                         cd_indica_obito = col_character(),
                         nm_municipio = col_character(),
                         nome_uf = col_character(),
                         ds_natureza_lesao = col_character(),
                         ds_cbo = col_character(),
                         ds_parte_corpo_atingida = col_character(),
                         cd_tipo_sexo_empregado_cat = col_character(),
                         ds_tipo_acidente = col_character(),
                         ds_tipo_local_acidente = col_character()
                     ))

# Remove codenames for localities and use better names for variables
#brasil <- brasil %>%
#    select(Nome_UF, Nome_Mesorregião, Nome_Microrregião, Nome_Município) %>%
#    rename(uf = Nome_UF,
#           mesorregiao = Nome_Mesorregião,
#           microrregiao = Nome_Microrregião,
#           municipio = Nome_Município)

# Use better variable names for dataset and put locality data in front
data <- rename(data, uf = nome_uf,
                  municipio = nm_municipio) %>%
    select(uf, municipio, everything())

# Add correponding locality data from brasil to data
complete <- brasil %>% inner_join(data, by = c("uf", "municipio"))
write.csv2(complete, "completo.csv", row.names=FALSE)

# Number of accidents:
country <- group_by(complete, pais) %>% summarize(acidentes = n())
by_region <- group_by(complete, regiao) %>% summarize(acidentes = n())
by_uf <- group_by(complete, uf) %>% summarize(acidentes = n())
by_meso <- group_by(complete, mesorregiao) %>% summarize(acidentes = n())
by_micro <- group_by(complete, microrregiao) %>% summarize(acidentes = n())
by_town <- group_by(complete, municipio) %>% summarize(acidentes = n())

# Write the summaries
write.csv2(country, "acidentes-total.csv", row.names=FALSE)
write.csv2(by_region, "acidentes-regiao.csv", row.names=FALSE)
write.csv2(by_uf, "acidentes-uf.csv", row.names=FALSE)
write.csv2(by_meso, "acidentes-meso.csv", row.names=FALSE)
write.csv2(by_micro,"acidentes-micro.csv", row.names=FALSE)
write.csv2(by_town, "acidentes-municipio.csv", row.names=FALSE)

# Put everything accident alongside locality (this is temporary)
acidentes <- brasil %>% inner_join(country, by = c("pais")) %>%
    inner_join(by_region, by = c("regiao")) %>%
    inner_join(by_uf, by = c("uf")) %>%
    inner_join(by_meso, by = c("mesorregiao")) %>%
    inner_join(by_micro, by = c("microrregiao")) %>%
    inner_join(by_town, by = c("municipio")) %>%
    select(pais,
           total = acidentes.x,
           regiao,
           acidentes_regiao = acidentes.y,
           uf,
           acidentes_uf = acidentes.x.x,
           mesorregiao,
           acidentes_meso = acidentes.y.y,
           microrregiao,
           acidentes_micro = acidentes.x.x.x,
           municipio,
           acidentes_municipio = acidentes.y.y.y)

write.csv2(acidentes, "acidentes.csv", row.names=FALSE)
